<meta title="页面映射配置" args="data"/>
<style>

</style>
<form class="form-horizontal" id="form" s="form,valid">
    <table class="table table-bordered table-condensed table-striped">
        <thead>
        <tr>
            <th>名称</th>
            <th>CODE</th>
            <th>跳转地址</th>
            <th>帮助</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody s="nda,loop" id="mappingList" s-data="data.urlMappingList">
        <tr s-loop-role="empty">
            <td colspan="5" class="alert alert-warning">暂无映射</td>
        </tr>
        <tr s="row,datac" s-loop-role="row">
            <td>
                <input autocomplete="off" placeholder="title" type="text" s-valid-error="require()" class="form-control title input-sm" name="title" />
            </td>
            <td>
                <input autocomplete="off" placeholder="code" type="text" s-valid-error="require()" class="form-control code input-sm" name="code" />
            </td>
            <td>
                <textarea name="url" class="form-control url" s-valid-error="require()"></textarea>
            </td>
            <td>
                <textarea name="help" class="form-control help" rows="2"></textarea>
            </td>
            <td>
                <input autocomplete="off" type="button" s-click="del(this)" class="btn btn-sm btn-default" value="删除" />
            </td>
        </tr>
        </tbody>
        <tfoot>
        <tr class="form-inline">
            <td colspan="5">
                <select class="form-control input-sm" s="select"
                        id="urlMappingCodes"
                        name="type"
                        s-build="S.get('/api/enum/valuesMap/core.enums.UrlMappingCodes')">
                    <option class="">空白</option>
                </select>
                <input autocomplete="off" type="button" class="btn btn-sm btn-default" s-click="addNew()" value="新增" />
            </td>
        </tr>
        </tfoot>
    </table>
</form>
<script type="text/javascript">
    S.getContent = function () {
        var deferred = $.Deferred();
        $form.validate().done(function() {
            var obj = {
                urlMappingList: []
            }
            var titleNodes = $form.node.find(".title");
            var codeNodes = $form.node.find(".code");
            var urlNodes = $form.node.find(".url");
            var helpNodes = $form.node.find(".help");
            titleNodes.each(function(i) {
                obj.urlMappingList.push({
                    title: $(this).val(),
                    code: codeNodes.get(i).value,
                    url: urlNodes.get(i).value,
                    help: helpNodes.get(i).value
                })
            })
            deferred.resolve(obj)
        })
        return deferred;
    }
    function addNew() {
        var mapping = $urlMappingCodes.getSelectData();
        if(mapping != null) {
            S.get("/api/urlMappingCodes/" + mapping.id).done(function(um) {
                $mappingList.addRow( {title: mapping.title, code: mapping.id, help: um.help}, 'append')
            })
        } else {
            $mappingList.addRow( {}, 'append')
        }

    }
    function del($s) {
        $s.parent().node.remove();
    }
</script>